package com.aote.util;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Iterator;

import javax.management.Attribute;
import javax.servlet.http.HttpServletRequest;

import com.browsesoft.resource.LicensePolicyTool;
import org.w3c.dom.Element;

import com.browsesoft.Entity;
import com.browsesoft.ExtendElement;
import com.browsesoft.htmlcomponent.HTMLBasicComponent;
import com.browsesoft.htmlcomponent.HTMLBasicModel;
import com.browsesoft.htmlcomponent.HTMLFormModel;
import com.browsesoft.htmlcomponent.HTMLPage;

/**
 * 组件帮助类
 * 
 */
public class ComponentHelper {
	public static HttpServletRequest selrequest;

	/**
	 * 返回找到得某个类名的实例组件 如果有多个，第一个找到的
	 */
	public static HTMLBasicComponent findComponentInstance(
			Iterator componentsIter, String className) {
		while (componentsIter.hasNext()) {
			HTMLBasicComponent comp = (HTMLBasicComponent) componentsIter
					.next();
			String compClassName = comp.getClass().getName();
			if (compClassName.equals(className)) {
				return comp;
			}
		}
		return null;
	}

	/**
	 * 返回父组件路径上，某个类型的组件
	 */
	public static HTMLBasicComponent getComponent(HTMLBasicComponent child,
			Class c) {
		HTMLBasicComponent comp = child;
		while (comp != null && !c.isInstance(comp)) {
			comp = comp.getParent();
		}
		return comp;
	}

	/**
	 * 找属性
	 * 
	 * @param comp
	 * @param name
	 * @return
	 */
	public static String getAttribute(HTMLBasicComponent comp, String name) {
		try {
			String result = new String();
			// 根据属性名从组件中找
			Attribute attribute = getComponentAttribute(comp, name);
			if (attribute != null) {
				result = (String) attribute.getValue();
				return result;
			}
			// 从组建对应的模型中找
			attribute = getModelAttribute(comp, name);
			if (attribute != null) {
				result = (String) attribute.getValue();
				return result;
			}
			// 从组件的父组件的模型中找
			HTMLBasicComponent component = comp.getParent();
			attribute = getModelAttribute(component, name);
			if (attribute != null) {
				result = (String) attribute.getValue();
				return result;
			}
			// 从request中找
			result = selrequest.getParameter(name);
			if (result != null) {
				return result;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return null;
	}

	/**
	 * 由组件和属性名得属性值
	 * 
	 * @throws NoSuchFieldException
	 * @throws Exception
	 */
	private static Attribute getComponentAttribute(HTMLBasicComponent comp,
			String name) throws Exception {
		Attribute attribute;
		String str = new String();
		Field field = null;
		Method method = null;
		// 从组件的类变量中找
		try {
			field = comp.getClass().getField(name);
			if (field != null) {
				str = field.get(comp).toString();
				if (str != null) {
					attribute = new Attribute(name, str);
					return attribute;
				}
			}
			// field为空
		} catch (NoSuchFieldException e) {
			// 从类方法找
			String newName = StringHelper.upperFirstCharacter(name);
			try {
				method = comp.getClass().getMethod(newName, new Class[] {});
				if (method != null) {
					str = method.invoke(comp, new Object[] {}).toString();
					if (str != null) {
						attribute = new Attribute(name, str);
						return attribute;
					}
				}
			} catch (NoSuchMethodException ex) {
				return null;
			}
		}
		return null;
	}

	/**
	 * 由模型和属性名得属性
	 * 
	 * @throws Exception
	 */
	private static Attribute getModelAttribute(HTMLBasicComponent comp,
			String name) throws Exception {
		HTMLBasicModel model = comp.getModel();
		// 从HTMLFormModel模型中找属性
		if (model instanceof HTMLFormModel) {
			String str = (String) ((HTMLFormModel) model).getAttributes().get(
					name);
			if (str != null) {
				Attribute attribute = new Attribute(name, str);
				return attribute;
			}
		}
		return null;
	}

	/**
	 * 得到该组件下,某种类型的子组件,返回第一个,包括自己
	 */
	public static HTMLBasicComponent getChildComponent(
			HTMLBasicComponent parent, Class c) {
		if (c.isInstance(parent)) {
			return parent;
		}
		Iterator iter = parent.getChildrenComponents();
		while (iter.hasNext()) {
			HTMLBasicComponent comp = (HTMLBasicComponent) iter.next();
			HTMLBasicComponent result = getChildComponent(comp, c);
			if (result != null) {
				return result;
			}
		}
		return null;
	}

	/**
	 * 找组件的父路径上的实体
	 */
	public static Entity getFormEntity(HTMLBasicComponent component) {
		if (component != null && !(component.getModel() instanceof Entity)) {
			Entity result = getFormEntity(component.getParent());
			return result;
		} else if (component == null) {
			return null;
		}
		return (Entity) component.getModel();
	}

	/**
	 * 处理需要隐藏的元素
	 */
	public static void disposeHidden(HttpServletRequest request,
			HTMLBasicComponent component) {
		throw new UnsupportedOperationException();
	}
}
